package com.shoeHouse.departmentInfo.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shoeHouse.departmentInfo.model.DepartmentInfo;
import com.shoeHouse.departmentInfo.service.DepartmentInfoService;
import com.shoeHouse.role.action.SelectTree;

import com.shoeHouse.role.model.RoleInfo;
import com.shoeHouse.user.model.UserInfo;
import com.shoeHouse.util.DataGrid;
import com.shoeHouse.util.IdWork;
import com.shoeHouse.util.PageHelper;
import com.shoeHouse.util.ResultInfo;

@Controller
@RequestMapping("/DepartmentInfo")
public class DepartmentInfoAction {
	@Autowired
   private DepartmentInfoService departmentInfoService;
	@ResponseBody
	@RequestMapping("/treeView")
	public List<SelectTree> treeView(HttpSession session,HttpServletRequest request){
		
		PageHelper pageHelper=new PageHelper();
		
		//查询总数
		
		  DepartmentInfo departmentInfo=new DepartmentInfo();
		List<DepartmentInfo> list=departmentInfoService.getAll(request,departmentInfo,pageHelper);
	TreeUtil treeUtil=new TreeUtil();
	UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
	//
     List<SelectTree> roleList=treeUtil.RoleTree(list,userInfo.getDepartmentInfo().getPid());
        // SelectTree selectTree=roleList.get(0);
		
		return roleList;
		
		
		
	}
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request,HttpSession session,Map<String, Object> map) {
		UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
		if(userInfo.getRoleId().equals("1")||userInfo.getRoleId().equals("2")||userInfo.getRoleId().equals("3")){
	    	   
	    	   map.put("role","role");
	       }
    
		return "/pages/department/list.jsp";
	}

	@ResponseBody
	@RequestMapping("/view")
	public DataGrid view(HttpServletRequest request,HttpServletResponse response,Integer page,Integer rows){
		//http://localhost:8080/shoeHouse/userInfo/view?page=1&rows=10
		//page表示当前页，rows表示一次查询的条数
		PageHelper pageHelper=new PageHelper();
		pageHelper.setCurrentPage(page);
		pageHelper.setPageSize(rows);
		//查询总数
		   DataGrid dataGrid=new DataGrid();
	      DepartmentInfo departmentInfo=new DepartmentInfo();
		List<DepartmentInfo> list=departmentInfoService.getAll(request,departmentInfo,pageHelper);
		int count=departmentInfoService.count();
		dataGrid.setTotal(count);
		dataGrid.setRows(list);

		return dataGrid;
			
			
			
		}
		
	
		
		
	
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		 map.put("method","addSave");
		return "/pages/department/save.jsp";
		
	}

	@ResponseBody
	@RequestMapping(value ="/addSave")
	public ResultInfo addSave(DepartmentInfo departmentInfo,HttpServletRequest request){
		
	       Date date=new Date();
	       departmentInfo.setId(IdWork.nextId());
	       departmentInfo.setGmtCreate(date);
	       departmentInfo.setGmtModified(date);
	       departmentInfo.setStatus(Byte.valueOf("0"));
        departmentInfoService.save(departmentInfo);
	     
	     ResultInfo result=new ResultInfo();
	     result.setSuccess(0);
		return result;
	}
	
	
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
//		map.put("saveUrl", "/manage/apartment/editSave");
		DepartmentInfo departmentInfo=new DepartmentInfo();
		departmentInfo.setId(id);
	
		DepartmentInfo departmentInfos=departmentInfoService.getSingle(departmentInfo);
		map.put("model",departmentInfos);
		 map.put("method","editSave");
	    System.out.println("user");
		return "/pages/department/save.jsp";
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ResultInfo editSave(DepartmentInfo departmentInfo,HttpServletRequest request,@RequestParam(value = "uploadFile", required = false) MultipartFile file) {
	
		
		departmentInfo.setGmtModified(new Date());
		departmentInfoService.update(departmentInfo);
		ResultInfo result=new ResultInfo();
		result.setSuccess(0);

		return result;
	}



	
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ResultInfo remove(String ids) {
		ResultInfo info=new ResultInfo();
		departmentInfoService.removeAll(ids);
		info.setMsg("正常");
		info.setSuccess(0);

		return info;
	}
	
}
